<template>
  <div class="box full flex-row bgfff">
    <header>
      <div class="font-16 box-center logo">智能屋</div>
    </header>
    <main class="box1">
      <ul>
        <li class=" flex-col flex-middle box-sizing border-b">
          <i class="iconfont icon-yonghu margin-r10 font-16"></i>
          <input type="text" class="box1" v-model="mobile" placeholder="请输入账号">
        </li>
        <li class=" flex-col flex-middle box-sizing border-b">
          <i class="iconfont icon-mima1 font-22"></i>
          <input type="password" class="box1" v-model="password" @keyup.enter="login" placeholder="请输入密码">
        </li>
      </ul>
      <div class="handle c82 font-14">
        <div class="padding-lr20 f-l" @click="toRouter('register')">注册</div>
        <div class="padding-lr20 f-r" @click="toRouter('set_pwd')">忘记密码</div>
      </div>
      <footer class="box-center footer font-16 box-center">
        <div class="cfff box-center" @click="login">登录</div>
      </footer>
    </main>
  </div>
</template>

<script>
const wx = require("weixin-js-sdk");
import CONFIG from "../../js/CONFIG.js";
export default {
  name: "login",
  data() {
    return {
      mobile: "",
      password: ""
    };
  },
  created() {
    localStorage.clear(); // 清除用户所有保存数据
    let that = this;
    let url = window.location.href.split("?")[1];
    console.log("-------------" + JSON.stringify(window.location.href));
    if (url) {
      let obj = this.getRequest(url);
      if (this.isString(obj.token)) {
        this.setCache("token", obj.token);
        this.updateUserInfo();
        if (
          this.isString(obj.houseCode) &&
          obj.houseCode !== "" &&
          obj.houseCode !== null &&
          obj.houseCode !== undefined &&
          obj.houseCode !== "null"
        ) {
          // alert(obj.houseCode+'----'+window.location.href);
          // 用户信息
          that.http
            .get("user/userInfo")
            .then(res => {
              if (res.data.mobile) {
                console.log("11111111111111111houseCode");
                that.toRouter("houses_info", { houseCode: obj.housesCode });
              } else {
                console.log("2222222222222222");
                that.toRouter("user_info", { id: 1 });
              }
            })
            .catch(ret => {
              console.log("333333333333333333");
              this.toRouter("houses_info", { houseCode: obj.housesCode });
            });
        } else {
          console.log("444444444444444444444");
          this.toRouter("index");
        }
      } else {
        console.log("55555555555555555555token");
        this.isWechat();
      }
    } else {
      console.log("6666666666666666666666");
      this.isWechat();
    }
  },
  mounted() {},
  methods: {
    isWechat() {
      let that = this;
      this.alert("登录过期，请求重新授权。", 2, () => {
        that.JumpToWeCaht();
      });
    },
    JumpToWeCaht() {
      let THIS = this;
      let returnURL = CONFIG.WeChat.WeChatOauth;

      // THIS.setLCDATABEFORELOGO();

      let _uid = this.$route.query.yqid || 1;

      // return false
      window.location.href =
        "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
        CONFIG.WeChat.appid +
        "&redirect_uri=" +
        returnURL +
        "&response_type=code&scope=snsapi_userinfo&state=" +
        _uid +
        "#wechat_redirect";
    },
    // 登录
    login() {
      let that = this,
        mobile = this.mobile,
        password = this.password;

      // 验证手机格式
      if (!this.hasPhone()) return;

      // 验证密码格式
      if (password === "") {
        that.layer("请输入密码");
        return;
      }

      this.http
        .post("user/login", {
          mobile: mobile,
          password: password
        })
        .then(res => {
          // console.log('123='+JSON.stringify(res));
          if (res.code === 0) {
            that.setCache("userInfo", res.user);
            that.setCache("token", res.token);
            that.toRouter("index");
          }
        })
        .catch(res => {});
    },
    // 手机验证
    hasPhone() {
      let phone = this.mobile;
      if (phone === "") {
        this.layer("请输入手机号");
        return false;
      }
      if (!this.hasMobile(phone)) {
        this.layer("请输入正确格式的手机号");
        return false;
      }

      return true;
    }
  }
};
</script>

<style scoped lang="scss">
header {
  position: relative;
  height: 2rem;
  background: no-repeat url(../../image/loginbg.png) center center / cover;
  .logo {
    width: 1rem;
    height: 0.6rem;
    border-radius: 0.1rem;
    background: #fff;
    color: red;
    position: absolute;
    bottom: -0.1rem;
    left: 50%;
    margin-left: -0.5rem;
    box-shadow: 0 0 0.15rem #aaa;
  }
}
main {
  ul {
    padding: 0.2rem 0.2rem 0 0.2rem;
    li {
      // height: 0.45rem;
      padding: 0.48rem 0 0.14rem;
      display: flex;
      // justify-content: center;
      align-items: flex-end;
      div {
        padding-right: 0.08rem;
      }
    }
  }
  .handle {
    width: 100%;
    height: 0.56rem;
    line-height: 0.56rem;
  }
}
footer {
  padding-top: 0.6rem;
}
</style>
